package org.linlinjava.litemall.wx.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallCarBrand;
import org.linlinjava.litemall.db.domain.LitemallCarModel;
import org.linlinjava.litemall.db.domain.LitemallCarSeries;
import org.linlinjava.litemall.db.service.LitemallCarBrandService;
import org.linlinjava.litemall.db.service.LitemallCarModelService;
import org.linlinjava.litemall.db.service.LitemallCarSeriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 专题服务
 */
@RestController
@RequestMapping("/wx/carBrand")
@Validated
public class WxCarBrandController {

    private final Log logger = LogFactory.getLog(WxBrandController.class);

    @Autowired
    private LitemallCarBrandService brandService;

    @Autowired
    private LitemallCarSeriesService seriesService;

    @Autowired
    private LitemallCarModelService modelService;


//    /**
//     * 品牌列表
//     *
//     * @param page 分页页数
//     * @param limit 分页大小
//     * @return 品牌列表
//     */
//    @GetMapping("list")
//    public Object list(@RequestParam(defaultValue = "1") Integer page,
//                       @RequestParam(defaultValue = "10") Integer limit,
//                       @Sort @RequestParam(defaultValue = "add_time") String sort,
//                       @Order @RequestParam(defaultValue = "desc") String order) {
//        List<LitemallBrand> brandList = brandService.query(page, limit, sort, order);
//        return ResponseUtil.okList(brandList);
//    }

    @GetMapping("selectbrandList")
    public Object selectbrandList() {
        List<LitemallCarBrand> carBrand = brandService.selectbrandList();
//        Map<String,List<LitemallCarBrand>> map = new HashMap<>();
        Map<String, List<LitemallCarBrand>> map = carBrand.stream().collect(Collectors.groupingBy(LitemallCarBrand::getGroup));
        return map;
    }

    @GetMapping("selectSeriesList")
    public Object selectSeriesList(String brandId) {
        List<LitemallCarSeries> carBrand = seriesService.selectSeriesList(brandId);
//        Map<String,List<LitemallCarBrand>> map = new HashMap<>();
        Map<String, List<LitemallCarSeries>> map = carBrand.stream().collect(Collectors.groupingBy(LitemallCarSeries::getGroup));
        return map;
    }

    @GetMapping("selectModelList")
    public Object selectModelList(String seriesId ) {
        List<LitemallCarModel> carBrand = modelService.selectModelList(seriesId);
//        Map<String,List<LitemallCarBrand>> map = new HashMap<>();
        Map<String, List<LitemallCarModel>> map = carBrand.stream().collect(Collectors.groupingBy(LitemallCarModel::getGroup));
        return map;
    }

//    /**
//     * 品牌详情
//     *
//     * @param id 品牌ID
//     * @return 品牌详情
//     */
//    @GetMapping("detail")
//    public Object detail(@NotNull Integer id) {
//        LitemallBrand entity = brandService.findById(id);
//        if (entity == null) {
//            return ResponseUtil.badArgumentValue();
//        }
//
//        return ResponseUtil.ok(entity);
//    }
}